package chainOfResponsibility;

import gameBasics.GameObject;
import object.Iron;
import tank.bullet.Bullet;

public class BulletIronCollider implements Collider{

    /**
     * 炮弹与铁块的碰撞
     * @param o1 炮弹对象
     * @param o2 铁块对象
     */
    @Override
    public boolean collide(GameObject o1, GameObject o2) {
        if(o1 instanceof Bullet bullet && o2 instanceof Iron iron){
            return !collideWith(bullet, iron);
        } else if (o1 instanceof Iron && o2 instanceof Bullet) {
            return !collide(o2, o1);
        }

        return true;
    }

    /**
     * 炮弹与铁块的碰撞检测
     * @param bullet 炮弹对象
     * @param iron 铁块对象
     */
    public boolean collideWith(Bullet bullet, Iron iron) {
        if(bullet.getRectangle().intersects(iron.getRectangle())){
            bullet.die();   //炮弹消亡

            return true;    //相撞
        }

        return false;
    }

}
